package nl.arjanfrans.stopwatch;


/**
 * Created by arjan on 7/12/13.
 */
public class Stopwatch {
    private long start_time;
    private long stop_time;
    private long pause_time;
    private boolean paused;

    public Stopwatch() {
        reset();
    }

    public void start() {
        if(paused) {
            pause_time = getElapsedTime();
            stop_time = 0;
            start_time = System.currentTimeMillis();
            paused = false;
        }
    }

    public void pause() {
        if(!paused) {
            stop_time = System.currentTimeMillis();
            paused = true;
        }
    }

    public void reset() {
        start_time = 0;
        stop_time = 0;
        pause_time = 0;
        paused = true;
    }

    public long getElapsedTime() {
        if(paused) {
            return (stop_time - start_time) + pause_time;
        }
        else {
            return (System.currentTimeMillis() - start_time) + pause_time;
        }
    }

    public String getTimeText() {
        long millis = getElapsedTime();
        int minutes = (int) (millis / (1000 * 60));
        int seconds = (int) ((millis / 1000) % 60);
        int hours = (int) ((millis / (1000*60*60)) % 24);
        int milliseconds = (int) (millis % 1000);
        return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds);
    }

    public boolean isPaused() {
        return paused;
    }




}
